package piblanes;

import java.awt.geom.Point2D;
import piblanes.apache.Polygon2D;

/**
 *
 */
public abstract class GeometricalState {
    GeometricalState(Point2D coordinates_, double angle_, Polygon2D shape_) {
        this.coordinates = coordinates_;
        this.angle = angle_;
        this.canonicalShape = shape_;
    }

    public Point2D getCoordinates() {
        return coordinates;
    }

    public double getX() {
        return coordinates.getX();
    }

    public double getY() {
        return coordinates.getY();
    }

    public void setX(double x_) {
        this.coordinates.setLocation(x_, getY());
    }

    public void setY(double y_) {
        this.coordinates.setLocation(getX(), y_);
    }

    void setCoordinates(Point2D coordinates_) {
        this.coordinates = coordinates_;
    }

    void move(double shiftX, double shiftY) {
        coordinates.setLocation(coordinates.getX() + shiftX, coordinates.getY() + shiftY);
    }

    void move(Point2D shift) {
        move(shift.getX(), shift.getY());
    }

    public Polygon2D getMovedRotatedShape() {
        Polygon2D shape = new Polygon2D();
        for (int i = 0; i<canonicalShape.npoints; ++i) {
            double x = canonicalShape.xpoints[i];
            double y = canonicalShape.ypoints[i];
            Point2D newPoint = new Point2D.Double();
            newPoint.setLocation(coordinates.getX() + x*Math.cos(angle) - y*Math.sin(angle),
                                 coordinates.getY() + x*Math.sin(angle) + y*Math.cos(angle));
            shape.addPoint(newPoint);
        }
        return shape;
    }

    public double getAngle() {
        return angle;
    }

    void addAngle(double angle_) {
        setAngle(this.getAngle() + angle_);
    }

    void setAngle(double angle_) {
        this.angle = angle_;
    }

    public Polygon2D getShape() {
        return this.canonicalShape;
    }

    void setShape(Polygon2D shape_) {
        this.canonicalShape = shape_;
    }

    protected Point2D coordinates = new Point2D.Double(0,0);
    protected double angle = Math.PI/3;
    protected Polygon2D canonicalShape = new Polygon2D();
}
